/** 
 * 
 * @author geloin 
 * @date 2012-5-5 涓婂崍9:31:52 
 */
package com.wcs.spring.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wcs.spring.entity.User;
import com.wcs.spring.service.UserService;
import com.wcs.spring.util.Const;
import com.wcs.spring.util.Md5;

@Controller
@RequestMapping(value = "ph/sys")
public class PhLoginController {
	@Resource(name = "userService")
	private UserService userService;

	@RequestMapping(value = "toLogin")
	public ModelAndView toLogin(HttpServletResponse response) throws Exception {
		return new ModelAndView("ph/login");
	}

	@RequestMapping(value = "login")
	public @ResponseBody
	Map<String, Object> login(User user, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		HttpSession session = request.getSession();
		Integer flag = 1;
		String message = "";
		User user2 = userService.queryUserByParam(user);
		if (user2 != null) {
			if ("xy".equals(user2.getUserType()) || "js".equals(user2.getUserType())) {
				flag = 0;
				session.setAttribute(Const.SESSION_PH_USER, user2);
			}
			else {
				message = "请使用学生或老师的账号登陆！";
			}
		}
		else {
			message = "用户名或密码有误！";
		}
		map.put("flag", flag);
		map.put("message", message);
		return map;
	}

	@RequestMapping(value = "topasswordedit")
	public ModelAndView topasswordedit(Integer id, HttpServletRequest request) throws Exception {
		return new ModelAndView("ph/passwordedit");
	}

	@RequestMapping(value = "passwordedit")
	public @ResponseBody
	Map<String, Object> passwordedit(Integer id, String oldPassword, String newPassword, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Integer flag = 1;
		String message = "";
		HttpSession session = request.getSession();
		User user2 = (User) session.getAttribute(Const.SESSION_PH_USER);
		if (!Md5.MD5(oldPassword).equals(user2.getPassword())) {
			message = "输入的原密码不正确！";
		}
		else {
			User user = new User();
			user.setId(id);
			user.setPassword(newPassword);
			user.setUpdateTime(new Date());
			user.setUpdateUser(user2.getUserName());
			userService.updateUser(user);
			flag = 0;
		}

		map.put("flag", flag);
		map.put("message", message);
		return map;
	}

	@RequestMapping(value = "logout")
	public ModelAndView logout(User user, HttpServletRequest request) throws Exception {
		request.getSession().removeAttribute(Const.SESSION_PH_USER);
		return new ModelAndView("redirect:/ph/sys/toLogin.do");
	}

	@RequestMapping(value = "index")
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		return new ModelAndView("ph/index", map);
	}

	@RequestMapping(value = "classinfo")
	public ModelAndView classinfo(HttpServletResponse response) throws Exception {
		return new ModelAndView("ph/classinfo");
	}

	@RequestMapping(value = "cominfo")
	public ModelAndView cominfo(HttpServletResponse response) throws Exception {
		return new ModelAndView("ph/cominfo");
	}

	@RequestMapping(value = "order")
	public ModelAndView order(HttpServletResponse response) throws Exception {
		return new ModelAndView("ph/order");
	}

	@RequestMapping(value = "ordered")
	public ModelAndView ordered(HttpServletResponse response) throws Exception {
		return new ModelAndView("ph/ordered");
	}

	@RequestMapping(value = "useredit")
	public @ResponseBody
	Map<String, Object> useredit(User user, HttpServletRequest request) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String message = "";
		User user2 = (User) request.getSession().getAttribute(Const.SESSION_PH_USER);
		user.setUpdateTime(new Date());
		user.setUpdateUser(user2.getUserName());
		userService.updateUser(user);
		map.put("flag", 0);
		map.put("message", message);
		map.put("id", user.getId());
		return map;
	}

	@RequestMapping(value = "touseredit")
	public ModelAndView touseredit(Integer id, HttpServletRequest request) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = userService.queryUserById(id);
		map.put("user", user);
		return new ModelAndView("ph/useredit", map);
	}

	@RequestMapping(value = "userinfo")
	public ModelAndView userinfo(Integer id, HttpServletRequest request) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = userService.queryUserById(id);
		map.put("user", user);
		return new ModelAndView("ph/userinfo", map);
	}

}
